package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Article;
import cn.xgj.sfzy.entity.ArticleType;
import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.mapper.ArticleMapper;
import cn.xgj.sfzy.mapper.ArticleTypeMapper;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月21日 14:40
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper mapper;
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public int add(Article article) {
        return mapper.insert(article);
    }

    @Override
    public int edit(Article article) {
        return mapper.update(article);
    }

    @Override
    public int remove(Integer id) {

        return mapper.delete(id);
    }

    @Override
    public List<Article> findAll(Article article) {
        return mapper.find(article);
    }

    @Override
    public Article findById(Integer id) {
        Article article = mapper.findById(id);
        Integer consultId = article.getConsultId();
        Consult consult = consultMapper.selectById(consultId);
        article.setConsult(consult);
        return article;
    }

    @Override
    public PageInfo<Article> findByPage(Integer pageNum, Integer pageSize, Article article) {
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list = mapper.find(article);
        for (Article at : list){
            Integer id = at.getArticleTypeId();
            ArticleType articleType = articleTypeMapper.findById(id);
            at.setArticleType(articleType);
            Integer consultId = at.getConsultId();
            Consult consult = consultMapper.selectById(consultId);
            at.setConsult(consult);
        }
        PageInfo<Article> info = new PageInfo<Article>(list);
        return info;
    }

}

